package site.web;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;

import neoutil.Config;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import site.entity.Resource;
import site.helper.CommonHelper;
import site.helper.FtpClient;
import site.helper.ZipUtils;
import site.service.ResourceRepository;

public class UploadTimerTask extends TimerTask {
	private static Date frontTime = new Date();// 上次执行时间
	private static Logger log = Logger.getLogger(UploadTimerTask.class);
	private int interval; // 间隔时间
	private ResourceRepository reposity;

	public UploadTimerTask(int interval) {
		this.interval = interval;
		reposity = new ResourceRepository();
	}

	@Override
	public void run() {
		Calendar newCal = Calendar.getInstance();
		Calendar oldCal = Calendar.getInstance();
		Calendar doTime = Calendar.getInstance();
		oldCal.add(Calendar.MILLISECOND, -interval);
		frontTime = oldCal.getTime();
		// 23:50执行
		doTime.set(oldCal.get(Calendar.YEAR), oldCal.get(Calendar.MONTH), oldCal.get(Calendar.DATE), 23, 50, 00);
		// 23:50在老时间和新时间之间的时候，执行。
		if (doTime.getTimeInMillis() >= oldCal.getTimeInMillis() && doTime.getTimeInMillis() <= newCal.getTimeInMillis()) {
			upload();
			// frontTime = new Date();
		}
	}

	private void upload() {
		Date date = frontTime;
		log.info(System.currentTimeMillis() + " - 开始上传数据...");
		List<Resource> resourceList = reposity.getByDate(date);
		log.info("查找到" + resourceList.size() + "条数据记录");
		String resourceTempPath = (String) Config.getResourceTempPath();
		log.info(resourceTempPath);
		SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
		String dirName = fmt.format(date);
		File dir = new File(resourceTempPath + "/" + dirName);
		dir.mkdir();
		log.info("创建文件夹:" + dir.getAbsolutePath());
		if (resourceList.size() > 0) {
			log.info("开始打包数据...");
			for (Resource resource : resourceList) {
				if (!CommonHelper.IsNullOrEmpty(resource.getFileName())) {
					try {
						File from = new File(Config.getResourceSavePath() + resource.getFileName());
						File to = new File(resourceTempPath + "/" + dirName + "/S" + resource.getNormcode() + "." + CommonHelper.getExtension(resource.getFileName()));
						FileUtils.copyFile(from, to);
						log.info("拷贝成功：" + from.getAbsolutePath() + "==>" + to.getAbsolutePath());
					} catch (Exception e) {
						log.error("文件拷贝过程出现异常");
						e.printStackTrace();
					}
				}
			}

			try {
				ZipUtils.zip(resourceTempPath + "/" + dirName, resourceTempPath + "/" + dirName + ".zip");
				log.info("压缩成功.");
			} catch (Exception e) {
				log.error("压缩过程出现异常");
				e.printStackTrace();
			}
			try {
				FtpClient client = new FtpClient("cyp", "cyp", "222.28.72.194", 21);
				client.connectServer();
				client.upFile("/035", dirName + ".zip", resourceTempPath + "/" + dirName + ".zip");
				client.closeConnect();
				log.info("成功上传文件到Ftp！");
			} catch (Exception e1) {
				log.error("上传文件到Ftp失败！");
				e1.printStackTrace();
			}
		}
		try {
			FileUtils.deleteDirectory(new File(resourceTempPath + "/" + dirName));
			log.info("成功删除原文件！");
		} catch (IOException e) {
			log.error("删除原文件失败！");
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {

	}
}
